﻿@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<script>
    $(document).ready(function () {
        $(".vote").click(function () {
            var youtubeId = $(this).attr('data-id');
            var voteControlId = $(this).attr('id');

            FB.getLoginStatus(function (response) {
                if (response.status === 'connected') {
                    // connected
                    var facebookUserId = response.authResponse.userID;
                    VoteVideo(voteControlId, youtubeId, facebookUserId);
                } else if (response.status === 'not_authorized') {
                    // not_authorized
                    LoginVote();
                } else {
                    // not_logged_in
                    LoginVote();
                }
            });
        });
    });
    function LoginVote()
    {
        FB.login(function (response) {
            if (response.authResponse) {
                var token = response.authResponse.accessToken;
                FB.api('/me?access_token=' + token + '', function (response) {
                    VoteVideo(voteControlId, youtubeId, response.id);
                });
            } else {
                // cancelled
            }
        });
    }
    function VoteVideo(voteControlId,videoId, facebookId)
    {
        $.post('@Url.Content("~/Home/VoteVideo")', { youtubeId: videoId, facebookId: facebookId }, function (data) {
            if (data != null && data.Result == true) {
                $("#" + voteControlId).attr('disabled', 'disabled');
                shareToWall();
            } 
        },'json');
    }

    function shareToWall()
    {

    }
</script>

<section id="video1">
    <object width="560" height="315"><param name="movie" value="http://www.youtube.com/v/BuMBmK5uksg?version=3&amp;hl=en_GB"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/BuMBmK5uksg?version=3&amp;hl=en_GB" type="application/x-shockwave-flash" width="560" height="315" allowscriptaccess="always" allowfullscreen="true"></embed></object>
    <input type="button" id="vote1" class="vote" data-id="BuMBmK5uksg" value="Vote" />
</section>

<section id="video2">
    <object width="560" height="315"><param name="movie" value="http://www.youtube.com/v/xix0TFHGZ9c?version=3&amp;hl=en_GB"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/xix0TFHGZ9c?version=3&amp;hl=en_GB" type="application/x-shockwave-flash" width="560" height="315" allowscriptaccess="always" allowfullscreen="true"></embed></object>
    <input type="button" id="vote2" class="vote" data-id="xix0TFHGZ9c" value="Vote" />
</section>